<?php

namespace addons\diandi_place\Traits;

use addons\diandi_place\models\place\PlaceList;
use Yii;

/**
 * @method updateAll(array $array, array $array1)
 */
trait PlaceTrait
{

    public function afterSave($insert, $changedAttributes): void
    {
        $hotel_id = Yii::$app->request->input('hotel_id', 0);
        if ($hotel_id) {
            $hotel = PlaceList::find()->where(['id' => $hotel_id])->select(['id', 'type'])->one();
        } else {
            $hotel = PlaceList::find()->where(['store_id' => $this->store_id])->select(['id', 'type'])->one();
        }

        if ($hotel){
            $up['hotel_id'] = $hotel['id'];
        }

        $type_id = Yii::$app->request->input('type_id', 0);

        if (empty($type_id) && $hotel){
            $up['type_id'] = $hotel['type'];

        }else{
            $up['type_id'] = $type_id;
        }
        $this->updateAll($up,['id'=>$this->id]);
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub

    }
}